<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="群成员" prop="wxUserid">
        <el-input
          v-model="queryParams.wxUserid"
          placeholder="请输入群成员id,可能是ww_contact.wx_user_id,或者是ww_employee.wx_user_id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入群时间" prop="joinTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.joinTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择入群时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择群成员状态，1-正常 2-退群" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wework:member:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wework:member:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wework:member:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['wework:member:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="memberList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="群成员" align="left" width="240">
        <template slot-scope="scope">
          <div style="float: left">
            <el-avatar shape="square" :size="40" :src="scope.row.thumbAvatar" :key="scope.row.thumbAvatar" style="vertical-align: middle"></el-avatar>
          </div>
          <div style="float: left">
            <div style="margin-right: 10px;padding-left: 3px;">{{ scope.row.name }}</div>
            <div style="padding-left: 3px;">
              <el-tag size="mini" v-if="scope.row.type=='1'" style="margin-right: 3px;">员工</el-tag>
              <el-tag size="mini" v-if="scope.row.status=='2'" style="margin-right: 3px;">已退群</el-tag>
              <el-tag size="mini" v-if="scope.row.adminStatus=='1'" style="margin-right: 3px;">管理员</el-tag>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="所在其他群" width="240">
        <template slot-scope="scope">
          <div v-for="group in scope.row.otherGroupchatList">
            {{group.name}}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="入群时间" align="center" prop="joinTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.joinTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入群方式" align="center" prop="joinScene" :formatter="joinSceneFormat" width="180"/>
      <el-table-column label="邀请人" align="center" prop="invitorWxUserid" width="200"/>
      <el-table-column label="退群时间" align="center" prop="quitTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.quitTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【群成员】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="群成员状态，1-正常 2-退群" prop="id">
          <el-input v-model="form.id" placeholder="请输入群成员状态，1-正常 2-退群" />
        </el-form-item>
        <el-form-item label="客户群id ww_groupchat.id" prop="groupchatId">
          <el-input v-model="form.groupchatId" placeholder="请输入客户群id ww_groupchat.id" />
        </el-form-item>
        <el-form-item label="企业id   ww_corp.id" prop="corpId">
          <el-input v-model="form.corpId" placeholder="请输入企业id   ww_corp.id" />
        </el-form-item>
        <el-form-item label="群成员id,可能是ww_contact.wx_user_id,或者是ww_employee.wx_user_id" prop="wxUserid">
          <el-input v-model="form.wxUserid" placeholder="请输入群成员id,可能是ww_contact.wx_user_id,或者是ww_employee.wx_user_id" />
        </el-form-item>
        <el-form-item label="成员类型 1 - 企业成员 2 - 外部联系人" prop="type">
          <el-select v-model="form.type" placeholder="请选择成员类型 1 - 企业成员 2 - 外部联系人">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="外部联系人在微信开放平台的唯一身份标识" prop="unionid">
          <el-input v-model="form.unionid" placeholder="请输入外部联系人在微信开放平台的唯一身份标识" />
        </el-form-item>
        <el-form-item label="入群时间" prop="joinTime">
          <el-date-picker clearable size="small"
                          v-model="form.joinTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择入群时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入群方式。1 - 由群成员邀请入群" prop="joinScene">
          <el-input v-model="form.joinScene" placeholder="请输入入群方式。1 - 由群成员邀请入群" />
        </el-form-item>
        <el-form-item label="退群时间" prop="quitTime">
          <el-date-picker clearable size="small"
                          v-model="form.quitTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择退群时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="邀请者。ww_employee.wx_user_id 目前仅当是由本企业内部成员邀请入群时会返回该值" prop="invitorWxUserid">
          <el-input v-model="form.invitorWxUserid" placeholder="请输入邀请者。ww_employee.wx_user_id 目前仅当是由本企业内部成员邀请入群时会返回该值" />
        </el-form-item>
        <el-form-item label="群成员状态，1-正常 2-退群">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMember, getMember, delMember, addMember, updateMember, exportMember } from "@/api/wework/groupchatMember";
import Template from "@/views/multitenant/template";

export default {
  name: "Member",
  components: {
    Template
  },
  data() {
    return {
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【群成员】表格数据
      memberList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupchatId: undefined,
        corpId: undefined,
        wxUserid: undefined,
        type: undefined,
        unionid: undefined,
        joinTime: undefined,
        joinScene: undefined,
        quitTime: undefined,
        invitorWxUserid: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          { required: true, message: "成员类型 1 - 企业成员 2 - 外部联系人不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    const groupchatId = this.$route.params && this.$route.params.groupchatId;
    this.queryParams.groupchatId = groupchatId;
    this.getDicts("groupchat_wx_join_scene").then(response => {
      this.joinSceneOptions = response.data;
    });
    this.getList();
  },
  methods: {
    /** 查询【群成员】列表 */
    getList() {
      this.loading = true;
      listMember(this.queryParams).then(response => {
        this.memberList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 状态字典翻译
    joinSceneFormat(row, column) {
      return this.selectDictLabel(this.joinSceneOptions, row.joinScene);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        corpId: undefined,
        wxUserid: undefined,
        type: undefined,
        unionid: undefined,
        joinTime: undefined,
        joinScene: undefined,
        quitTime: undefined,
        createTime: undefined,
        invitorWxUserid: undefined,
        updateTime: undefined,
        status: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【群成员】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getMember(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改【群成员】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateMember(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMember(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除【群成员】编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        return delMember(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有【群成员】数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportMember(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
